# -*- coding: utf-8 -*-
import codecs
from datetime import datetime
from decimal import *
import re
import urllib
import time

from pyquery import PyQuery
import requests
from requests.exceptions import ConnectionError

from service.models import Prices
from settings import db


special_prices = ['Да', 'Нет']


class UrlParsing():
    url = ('http://hotline.ua/',)
    productsName = {}
    productsPrice = {}
    productsStatus = {}
    productsOwner = {}
    productsStatusPrice = {}
    productsError = []

    def __init__(self, status):
        self.getAllProductsName(status)
        self.getAllProductsPrice()
        self.getAllProductsStatus()

    def getAllProductsName(self, status):
        if status:
            products = db.sqlQuery("""SELECT value, entity_id FROM firstcatalog_product_entity_varchar where attribute_id =
                (SELECT attribute_id FROM firsteav_attribute where attribute_code='name' and entity_type_id=4)
                and entity_id in (SELECT entity_id FROM firstcatalog_product_entity_int where attribute_id =96 and value=1);""")
            for product in products:
                self.productsName[product[1]] = product[0].lower()
                owner = db.sqlQuery("""SELECT value FROM firsteav_attribute_option_value where option_id=
                (SELECT value FROM firstcatalog_product_entity_int where entity_id=""" + str(product[1]) +
                                    """ and attribute_id=(SELECT attribute_id FROM firsteav_attribute where attribute_code='owner'))""")
                if owner:
                    owner = owner[0][0]
                else:
                    owner = ''
                self.productsOwner[product[1]] = owner
        else:
            products = db.sqlQuery("""SELECT value, entity_id FROM firstcatalog_product_entity_varchar where attribute_id =
                (SELECT attribute_id FROM firsteav_attribute where attribute_code='name' and entity_type_id=4)
                and entity_id in (SELECT entity_id FROM firstcatalog_product_entity_int where attribute_id =96 and value=2);""")
            for product in products:
                self.productsName[product[1]] = product[0].lower()
                owner = db.sqlQuery("""SELECT value FROM firsteav_attribute_option_value where option_id=
                (SELECT value FROM firstcatalog_product_entity_int where entity_id=""" + str(product[1]) +
                                    """ and attribute_id=(SELECT attribute_id FROM firsteav_attribute where attribute_code='owner'))""")
                if owner:
                    owner = owner[0][0]
                else:
                    owner = ''
                self.productsOwner[product[1]] = owner

    def getAllProductsPrice(self):
        products = db.sqlQuery("""SELECT value, entity_id FROM firstcatalog_product_entity_decimal where attribute_id =
            (SELECT attribute_id FROM firsteav_attribute where entity_type_id=4 and attribute_code='price')
            and entity_id in (SELECT entity_id FROM firstcatalog_product_entity_int where attribute_id =96 and value=1);""")
        i = 1
        for product in products:
            if product[1]==2088:
                pass
            superPrice = db.sqlQuery("""SELECT value FROM firstcatalog_product_entity_decimal where attribute_id =
            (SELECT attribute_id FROM firsteav_attribute where entity_type_id=4 and attribute_code='special_price') and entity_id=""" + str(
                product[1]) + """;""")
            self.productsPrice[product[1]] = product[0]
            self.productsStatusPrice[product[1]] = 'Нет'
            superDate = db.sqlQuery("""SELECT value FROM firstcatalog_product_entity_datetime where attribute_id in
            (SELECT attribute_id FROM firsteav_attribute where attribute_code in ('special_from_date', 'special_to_date')) and entity_id=%s""" % str(
                product[1]))

            if superPrice[0][0]:
                if superPrice[0][0] < product[0]:
                    if superDate:
                        if self.trueDatetime(superDate[0][0], superDate[1][0]):
                            print i
                            i += 1
                            self.productsPrice[product[1]] = superPrice[0][0]
                            self.productsStatusPrice[product[1]] = 'Да'

    def trueDatetime(self, dateFrom, dateTo):
        if dateFrom and dateTo:
            if (dateFrom < datetime.now()) and (dateTo > datetime.now()):
                return True
            else:
                return False
        elif dateFrom:
            if dateFrom < datetime.now():
                return True
            else:
                return False
        elif dateTo:
            if dateTo > datetime.now():
                return True
            else:
                return False
        else:
            return False


    def getAllProductsStatus(self):
        products = db.sqlQuery("""SELECT value, entity_id FROM firstcatalog_product_entity_int where attribute_id =
            (SELECT attribute_id FROM firsteav_attribute where entity_type_id=4 and attribute_code='status')
            and entity_id in (SELECT entity_id FROM firstcatalog_product_entity_int where attribute_id =96 and value=1);""")
        for product in products:
            if product[0] == 1:
                status = True
            else:
                status = False
            self.productsStatus[product[1]] = status


    def definitionParser(self, url=None):
        for url in self.url:
            if url == 'http://hotline.ua/':
                self.hotlineParser(url)

    proxies = {
        "http": '188.231.227.220:3128',
        "http": '77.122.65.195:8080',
        "http": '82.209.64.125:8080',
        "http": '94.125.90.6:8080',
        "http": '80.249.133.118:80',
        "http": '212.33.250.197:3128',
        "http": '212.24.47.185:8080',
        "http": '109.162.48.241:3128',
        "http": '95.178.6.190:8088',
        "http": '46.161.145.229:8080',
        "http": '62.121.64.19:8080',
        "http": '188.234.132.2:8080',
        "http": '109.226.199.129:8080',
        }

    def hotlineParser(self, host):
        """

        """
        productsError = []
        for key in self.productsName.keys():
            name = self.productsName[key]
            try:
                response = requests.get(host + 'sr/?q=' + urllib.quote_plus(name))
            except ConnectionError:
                productsError.append(link)
                self.saveValue(key=key)
            link = host + 'sr/?q=' + urllib.quote_plus(name)
            if response.status_code == 200:
                self.parserHtml(response, key, link)
            else:
                productsError.append(link)
                self.saveValue(key=key)
        if productsError:
            for productLink in productsError:
                while response.status_code == 200:
                    response = requests.get(host + 'sr/?q=' + urllib.quote_plus(name))
                self.parserHtml(response, key, link)

        return True

    def parserHtml(self, response, key, link):
        priceAvg, priceMin, priceMax = -1, -1, -1
        try:
            jQuery = PyQuery(response.text)
            pricesHtml = jQuery('.misc span')
            if not pricesHtml:
                priceAvgHtml = jQuery('.price a').text()
                if priceAvgHtml:
                    pr = priceAvgHtml.encode('utf-8').split('грн Купить')
                    if len(pr) == 2:
                        priceAvg = Decimal(pr[0].replace(' ', '').replace(' ', '')).quantize(Decimal('.01'),
                            rounding=ROUND_DOWN)
                    elif len(pr) > 2:
                        totalPrice = 0
                        countPrice = 0
                        priceMin = Decimal(
                            pr[0].replace(' ', '').replace(' ', '').replace('Купить', '')).quantize(
                            Decimal('.01'),
                            rounding=ROUND_DOWN)
                        priceMax = priceMin
                        for price in pr:
                            if price != '':
                                price = Decimal(
                                    price.replace(' ', '').replace(' ', '').replace('Купить', '')).quantize(
                                    Decimal('.01'),
                                    rounding=ROUND_DOWN)
                                totalPrice += price
                                countPrice += 1
                                if priceMin > price:
                                    priceMin = price
                                elif priceMax < price:
                                    priceMax = price
                        priceAvg = (totalPrice / countPrice).quantize(Decimal('.01'), rounding=ROUND_DOWN)

            elif pricesHtml:
                if len(pricesHtml) > 1:
                    i = 0
                    for html in jQuery('.main h2 a'):
                        if re.sub(r'^[a-zA-Z0-9]$', '', self.productsName[key].upper().replace(' ', '')) == re.sub(
                            r'^[a-zA-Z0-9]$', '', html.text.encode('utf-8').upper().replace(' ', '')):
                            html_ = pricesHtml.eq(i).text().encode('utf-8').replace('\t', '').replace(' ',
                                '').replace('грн',
                                '').replace(' ', '')
                        i += 1
                else:
                    html_ = pricesHtml.text().encode('utf-8').replace('\t', '').replace(' ', '').replace('грн',
                        '').replace(' ', '')
                if html_:
                    prices = html_.split('/\n')
                    if len(prices) == 2:
                        priceAvg = Decimal(prices[0]).quantize(Decimal('.01'), rounding=ROUND_DOWN)
                        prices = prices[1].split('-')
                        priceMin = Decimal(prices[0]).quantize(Decimal('.01'), rounding=ROUND_DOWN)
                        priceMax = Decimal(prices[1]).quantize(Decimal('.01'), rounding=ROUND_DOWN)
                    elif len(prices) == 1:
                        price = Decimal(prices[0]).quantize(Decimal('.01'), rounding=ROUND_DOWN)
        except Exception:
            logError(link)
        self.saveValue(key, priceAvg, priceMin, priceMax)

    #        time.sleep(5)


    def saveValue(self, key, priceAvg=-1, priceMin=-1, priceMax=-1):
        """

        """

        product = Prices.objects.filter(entity_id=key)

        if product:
            product = product[0]
            product.name = self.productsName[key]
            product.price = self.productsPrice[key]
            product.status = self.productsStatus[key]
            product.owner = self.productsOwner[key]
            product.special_price = 0 if self.productsStatusPrice[key] == 'Да' else 1
            product.priceAvg = priceAvg
            product.priceFrom = priceMin
            product.priceTo = priceMax
        else:
            product = Prices(entity_id=key, name=self.productsName[key], price=self.productsPrice[key],
                priceFrom=priceMin, priceTo=priceMax, priceAvg=priceAvg, status=self.productsStatus[key],
                owner=self.productsOwner[key], special_price=0 if self.productsStatusPrice[key] == 'Да' else 1)
        product.save()


def getProducts(filt=None):
    """

    """
    productsPrice = Prices.objects.all()
    if filt:
        if filt.has_key('name'):
            if filt['name']:
                productsPrice = productsPrice.filter(name__contains=filt['name'].encode('utf-8').lower())
        if filt.has_key('price'):
            if filt['price'].isdigit():
                productsPrice = productsPrice.filter(price=Decimal(filt['price']))
        if filt.has_key('priceFrom'):
            if filt['priceFrom'].isdigit():
                productsPrice = productsPrice.filter(priceFrom__gt=Decimal(filt['priceFrom']))
        if filt.has_key('priceTo'):
            if filt['priceTo'].isdigit():
                productsPrice = productsPrice.filter(priceTo__lt=Decimal(filt['priceTo']))
        if filt.has_key('priceAvg'):
            if filt['priceAvg'].isdigit():
                productsPrice = productsPrice.filter(priceAvg=Decimal(filt['priceAvg']))
        if filt.has_key('status'):
            if filt['status']:
                status = 1 if filt['status'].encode('utf-8') == 'включен' else 0
                productsPrice = productsPrice.filter(status=status)
        if filt.has_key('special_price'):
            if filt['special_price']:
                status = 0 if filt['special_price'].encode('utf-8') == 'Да' else 1
                productsPrice = productsPrice.filter(special_price=status)
        if filt.has_key('owner'):
            if filt['owner']:
                productsPrice = productsPrice.filter(owner=filt['owner'])
        if filt.has_key('sort'):
            sort = '' if not filt.has_key('sorting') else filt['sorting']
            productsPrice = productsPrice.order_by(sort + filt['sort'].encode('utf-8'))
    products = []
    for productPrice in productsPrice:
        product = dict(name=productPrice.name, price=productPrice.price, priceFrom=productPrice.priceFrom,
            priceTo=productPrice.priceTo, priceAvg=productPrice.priceAvg)
        product['diffPriceFrom'] = product['price'] - product['priceFrom']
        product['diffPriceTo'] = product['priceTo'] - product['price']
        product['status'] = 'включен' if productPrice.status == 1 else 'выключен'
        product['special_price'] = 'Да' if productPrice.special_price == 0 else 'Нет'
        product['owner'] = productPrice.owner
        div = (product['priceTo'] - product['priceFrom']) / 3
        if product['priceAvg'] == -1:
            product['type'] = 'gray'
        elif product['priceFrom'] == -1:
            product['type'] = 'green' if product['price'] < product['priceAvg'] else 'red'
        else:
            product['type'] = 'green' if product['price'] < product['priceFrom'] + div else 'yellow'\
            if (product['price'] >= product['priceFrom'] + div) and (
                product['price'] < product['priceTo'] - div) else 'red'
        if filt:
            if filt['color'] == product['type']:
                products.append(product)
            elif filt['color'] == '':
                products.append(product)
        else:
            products.append(product)
    if filt:
        return products, filt
    return products


def getFiltrUrl(filt):
    url = '?'
    for param in filt:
        if url != '?':
            url += '&'
        url += param + '=' + filt[param]
    return url


def getOwners():
    owners = []
    ownersName = db.sqlQuery('SELECT role_name FROM firstadmin_role where parent_id=5')
    for ownerName in ownersName:
        owners.append(ownerName[0])
    return owners


def logError(link):
    files = codecs.open('badLink.log', 'a', encoding="utf-8")
    files.write(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + ":" + link + "\r\n")
    files.close()

